package com.action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.ComonDao;
import com.model.Gz;
import com.model.Tz;
import com.model.User;

@Controller
@RequestMapping("/XtAction")
public class XtAction {
	@Resource(name = "ComonDao")
	private ComonDao commonDao;
	@ResponseBody
	@RequestMapping(value = "/logout", produces = "application/json;charset=UTF-8")
	public Map<String,String> logout(HttpSession httpSession) {
		Map<String,String> map=new 	HashMap<String,String>();
		httpSession.removeAttribute("username");
		map.put("returnCode", "0");
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getUsername", produces = "application/json;charset=UTF-8")
	public Map<String,String> getUsername(HttpSession httpSession) {
		Map<String,String> map=new 	HashMap<String,String>();
			
			String username=(String)httpSession.getAttribute("username");
			if(username!=null){
			map.put("returnCode", "0");
			map.put("username", username);
			return map;
			}
			map.put("returnCode", "1");
			return map;
	}
	@ResponseBody
	@RequestMapping(value = "/login", produces = "application/json;charset=UTF-8")
	public Map<String,String> login(HttpSession httpSession, String username,String password) {
		Map<String,String> map=new 	HashMap<String,String>();
		if(commonDao.login(username,password)){
			
			httpSession.setAttribute("username", username);
			map.put("returnCode", "0");
			return map;
		}
		map.put("returnCode", "1");
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getUser", produces = "application/json;charset=UTF-8")
	public Map<String,Object> getUser(String page) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		int total= commonDao.getTotalUser();
		int totalPage=total%10==0?total/10:total/10+1;
		map.put("totalPage", totalPage);
		List<User> list=commonDao.findUserByPage(page);
		map.put("list", list);
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getGz", produces = "application/json;charset=UTF-8")
	public Map<String,Object> getGz(String page,String gzlb) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		int total= commonDao.getTotalGz(gzlb);
		int totalPage=total%10==0?total/10:total/10+1;
		map.put("totalPage", totalPage);
		List<Gz> list=commonDao.findGzByPage(page,gzlb);
		map.put("list", list);
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getTz", produces = "application/json;charset=UTF-8")
	public Map<String,Object> getTz(String page) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		int total= commonDao.getTotalTz();
		int totalPage=total%10==0?total/10:total/10+1;
		map.put("totalPage", totalPage);
		List<Tz> list=commonDao.findTzByPage(page);
		map.put("list", list);
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getUser1", produces = "application/json;charset=UTF-8")
	public Map<String,Object> getUser1(String id) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		map.put("user", commonDao.getUserById(id));
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/getGz1", produces = "application/json;charset=UTF-8")
	public Map<String,Object> getGz1(String id) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		map.put("gz", commonDao.getGzById(id));
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/deleteUser", produces = "application/json;charset=UTF-8") 
	public Map<String,Object> deleteUser(String id) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		commonDao.deleteUser(id.split("','"));
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/deleteGz", produces = "application/json;charset=UTF-8") 
	public Map<String,Object> deleteGz(String id) {
		HashMap<String,Object>map=new HashMap<String,Object>();
		commonDao.deleteGz(id.split("','"));
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "/saveUser", produces = "application/json;charset=UTF-8")
	public String saveUser(User user) {
		if(user.getId()!=null&&!user.getId().equals("")){ commonDao.updateUser(user);}else{
	    user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	    user.setId(getId());
	    commonDao.saveUser(user);
		}
		return "0";
	}
	@ResponseBody
	@RequestMapping(value = "/saveCzpl", produces = "application/json;charset=UTF-8")
	public String saveCzpl(Gz gz) {
		if(gz.getId()!=null&&!gz.getId().equals("")){ 
			gz.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			 commonDao.updateGz(gz);
		}
		else{
			gz.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			gz.setId(getId());
			commonDao.saveGz(gz);
		}
		return "0";
	}
	 public String getId() {  
	          
	        return UUID.randomUUID().toString().replace("-", "");  
	    } 
	 @ResponseBody
	 @RequestMapping(value = "/getFJdzy", produces = "application/json;charset=UTF-8")
	 public Map<String,List<String>> getFJdzy(HttpServletRequest request,String jdip)  throws UnsupportedEncodingException{
	     Map<String,List<String>> map=new HashMap<String,List<String>>();
	     commonDao.setJcData(jdip,map);
	 	return map;
	 }
	 
	 @ResponseBody
	 @RequestMapping(value = "/getJdzy", produces = "application/json;charset=UTF-8")
	 public Map<String,List<String>> getFJdzy(HttpServletRequest request,String ip,String startTime,String endTime)   throws UnsupportedEncodingException{
	 	Map<String,List<String>> map=new HashMap<String,List<String>>();
	 	commonDao.setJcData(ip,startTime,endTime,map);
	 	return map;
	 }
}
